﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneAppCheckSensors
{
    using Microsoft.Devices.Sensors;

    public partial class MainPage : PhoneApplicationPage
    {
        // コンストラクター
        public MainPage()
        {
            InitializeComponent();
        }

        private bool isLocationAccessAsked = false;
        private bool isLocationAccessEnabled = false;
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!isLocationAccessAsked && MessageBox.Show(
@"This application access 'Location API'.
This application shows your locatoin raw data got from 'Location API' and uses it as center position when show BingMap.
You can select this location access feature use or not.
If you push 'OK' then you can use all features of this application, if not you can use only features which doesn't access to 'Location API'.",
                                               "Location Access Notification",
                                               MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                isLocationAccessEnabled = true;
            }
            isLocationAccessAsked = true;

            if (Accelerometer.IsSupported)
            {
                buttonAccel.IsEnabled = true;
            }
            else
            {
                buttonAccel.IsEnabled = false;
            }

            if (Gyroscope.IsSupported)
            {
                buttonGyroscope.IsEnabled = true;
            }
            else
            {
                buttonGyroscope.IsEnabled = false;
            }

            if (Motion.IsSupported)
            {
                buttonMotion.IsEnabled = true;
            }
            else
            {
                buttonMotion.IsEnabled = false;
            }

            if (Compass.IsSupported)
            {
                buttonCompass.IsEnabled = true;
            }
            else
            {
                buttonCompass.IsEnabled = false;
            }

            if (isLocationAccessEnabled)
            {
                buttonLocation.IsEnabled = true;
            }
            else
            {
                buttonLocation.IsEnabled = false;
            }
        }

        private void buttonAccel_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(
                new Uri("/PageAccelerometer.xaml?NavigatedFrom=Main Page", UriKind.Relative));
        }

        private void buttonCompass_Click(object sender, RoutedEventArgs e)
        {
            string newPageUrl = "/PageCompass.xaml?NavigatedFrom=Main Page";
            if (cbShowVisual.IsChecked.Value)
            {
                newPageUrl = "/PageCompassVisual.xaml?NavigatedFrom=Main Page";
            }
            if (isLocationAccessEnabled)
            {
                newPageUrl += "&isLocationOK=true";
            }
            else
            {
                newPageUrl += "&isLocationOK=false";
            }
            this.NavigationService.Navigate(
                new Uri(newPageUrl, UriKind.Relative));
        }

        private void buttonGyroscope_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(
                new Uri("/PageGyroscope.xaml?NavigatedFrom=Main Page", UriKind.Relative));
        }

        private void buttonMotion_Click(object sender, RoutedEventArgs e)
        {
            string newPageUrl = "/PageMotion.xaml?NavigatedFrom=Main Page";
            if (cbShowVisual.IsChecked.Value)
            {
                newPageUrl = "/PageMotionVisual.xaml?NavigatedFrom=Main Page";
            }
            this.NavigationService.Navigate(
                new Uri(newPageUrl, UriKind.Relative));
        }

        private void buttonLocation_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(
                new Uri("/PageLocation.xaml?NavigatedFrom=Main Page", UriKind.Relative));
        }
    }
}
